home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / Item Class / Item sources / CItemList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  3.8 KB  |  223 lines  |  [TEXT/KAHL]

  1. /*
  2.  * File:        CItemList.c
  3.  * Created:        7/25/93
  4.  * Desc:        A list of CItems that can initialize itself
  5.  *                from disk.  See CItem for a full description of
  6.  *                what items are.
  7.  *
  8.  *                In resources, each subtree is stored separately
  9.  *
  10.  * Superclass:    CList.
  11.  * Uses:        CItem.
  12.  * Original Author:    Atul Barve
  13.  * Modifications:    W. Wesley Monroe
  14.  *
  15.  * Copyright © 1993 Animas Software Production. All rights reserved.
  16.  */
  17.  
  18. #include    "CItemList.h"
  19. #include    "CItem.h"
  20.  
  21. void    CItemList::IItemList(void)
  22. {
  23.     inherited::IList();
  24. }
  25.  
  26. /*
  27.  * ReadItem()
  28.  *
  29.  *    Recursively read an item from a resource.
  30.  */
  31.  
  32. CItem    *CItemList::ReadItem(Handle itemH, short level)
  33. {
  34.     CItem        *theItem;
  35.     CItem        *sub;
  36.  
  37.     tItem        **h;
  38.  
  39.     char        *c, text[256];
  40.     short        canExpand, iconID, expanded;
  41.     short        j, n, len;
  42.     short        numSubs;
  43.     
  44.     theItem = MakeItem();
  45.  
  46.     h = (tItem **) itemH;
  47.     expanded = (**h).expanded;
  48.     canExpand = (**h).canExpand;
  49.     iconID = (**h).iconID;
  50.     
  51.     BlockMove((**h).text, text, (**h).text[0]+1);
  52.  
  53.     theItem->SetExpanded(expanded);
  54.     theItem->SetDisplayText(text);
  55.     theItem->SetCanExpand(canExpand);
  56.     theItem->SetIconID(iconID);
  57.  
  58.         // Move the text pointer past the pascal string to get to
  59.         // the number of subitems...
  60.     c = (**h).text;
  61.     len = text[0];
  62.     len++;
  63.     len = (len % 2 == 1) ? len + 1:len;
  64.     c += len;
  65.  
  66.     *h = (tItem *) c;
  67.     theItem->GetExtraItemData((Handle) h);
  68.  
  69.     c = (char *) *h;
  70.     numSubs = * (short *) c;
  71.  
  72.     c += sizeof(short);
  73.  
  74.     *h = (tItem *) c;
  75.     for(j = 0;j < numSubs; j++) {
  76.         sub = ReadItem((Handle) h,level+1);
  77.         sub->SetParent(theItem);
  78.         theItem->SetLevel(level);
  79.         theItem->SetNthSubItem(sub,100);
  80.     }
  81.  
  82.     return theItem;
  83. }
  84.  
  85. /*
  86.  * MakeItem()
  87.  *
  88.  *    A Method that subclasses can override to put other
  89.  *    types of objects in the list.
  90.  */
  91.  
  92. CItem *CItemList::MakeItem(void)
  93. {
  94.     CItem *anItem;
  95.     
  96.     anItem = new CItem;
  97.     
  98.     return anItem;
  99. }
  100.  
  101. /*
  102.  * IItemRes()
  103.  *
  104.  *    Read the list from resource type 'type' resource.
  105.  */
  106.  
  107. void    CItemList::IItemRes(OSType type)
  108. {
  109.     CItem *theItem;
  110.  
  111.     Handle        hndl;
  112.     char        text[256];
  113.     OSType        rType;
  114.     short        rID,i,n, itemsRead;
  115.     
  116.     n = Count1Resources(type);
  117.  
  118.     for(i = 1; i <= n; i++) {
  119.         hndl = Get1IndResource(type, i);
  120.         FailResError();
  121.         HLock(hndl);
  122.         theItem = ReadItem(hndl, 0);
  123.         HUnlock(hndl);
  124.  
  125.         Append(theItem);
  126.     }
  127. }
  128.  
  129. CItemList *CItemList::CopyList(Boolean copySubItems)
  130. {
  131.     CItemList    *copyList;
  132.     CItem    *anItem, *copyItem;
  133.  
  134.     long i, n = GetNumItems();
  135.  
  136.     copyList = new CItemList;
  137.     copyList->IList();
  138.  
  139.     for(i = 1; i <= n; i++) {
  140.     
  141.         anItem = (CItem *) NthItem(i);
  142.         copyItem = anItem->CopyItem(copySubItems);
  143.         copyList->Append(copyItem);
  144.     }
  145.     
  146.     return copyList;
  147. }
  148.     
  149. /*void    CItemList::Dispose(void)*/
  150. /*{*/
  151. /*    DisposeItems();*/
  152. /**/
  153. /*    inherited::Dispose();*/
  154. /*}*/
  155.  
  156. /*
  157.  * RemoveFromVisTree()
  158.  *
  159.  *    Remove an item, and recurrsively its children
  160.  *    from the visible list, but do ***not***
  161.  *    dispose of the items themselves.
  162.  */
  163.  
  164. void    CItemList::RemoveFromVisTree(CObject *theObject)
  165. {
  166.     register long    i;
  167.     CItem            *aItem, *sub;
  168.     short            num;
  169.  
  170.     aItem = (CItem *) theObject;
  171.     num = aItem->GetNumSubItems();    
  172.     for(i=1;i<=num;i++) {
  173.         sub = aItem->GetNthSubItem(i);
  174.         sub->SetExpanded(0);
  175.         Remove(sub);
  176.     }
  177.  
  178.     aItem->SetExpanded(0);
  179.     
  180.     i = Offset(theObject);
  181.     if (i != BAD_INDEX)
  182.         DeleteItem( i+1);
  183.  
  184. }
  185.  
  186. /*
  187.  * RemoveItem()
  188.  *
  189.  *    Remove theObject and its recursively, its children
  190.  *    from the itemlist.
  191.  *    NOTE:    The parent still has a reference to theObject
  192.  *            in its subitems list.
  193.  */
  194.  
  195. void    CItemList::RemoveItem(CObject    *theObject)
  196. {
  197.     register long    i;
  198.     CItem            *aItem, *parent;
  199.     short            num;
  200.     CItem            *sub;
  201.  
  202.     aItem = (CItem *) theObject;
  203.     num = aItem->GetNumSubItems();    
  204.     for(i=1;i<=num;i++) {
  205.         sub = aItem->GetNthSubItem(i);
  206.         sub->SetExpanded(0);
  207.         Remove(sub);
  208.     }
  209.  
  210.     aItem->SetExpanded(0);
  211.     
  212.     i = Offset(theObject);
  213.     if (i != BAD_INDEX)
  214.         DeleteItem( i+1);
  215. }
  216.  
  217. void    CItemList::DeleteItems(void)
  218. {
  219.     while(numItems) {
  220.         DeleteItem(1);
  221.     }
  222. }
  223.